package com.dcits.account.service.impl;

import com.dcits.account.constants.BaseContants;
import com.dcits.account.entity.cto.InputObject;
import com.dcits.account.entity.cto.OutputObject;
import com.dcits.account.entity.po.AccountEntity;
import com.dcits.account.mapper.AccountEntityMapper;
import com.dcits.account.service.interfaces.IAccountService;
import com.dcits.account.util.BaseUtil;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * @Auther: 2843896844@qq.com
 * @Date: 2021/06/11/19:51
 * @Description:
 */
@Service
public class AccountServiceImpl implements IAccountService {

    private Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Autowired
    private AccountEntityMapper accountEntityMapper;

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void initNewAccount(InputObject inputObject, OutputObject outputObject) {
        Map<String, Object> params = inputObject.getParams();
        Integer userId = MapUtils.getInteger(params, "id");
        String userPhone = MapUtils.getString(params, "userPhone");
        String password = MapUtils.getString(params, "password");
        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setUserId(userId);
        accountEntity.setUserPhone(userPhone);
        accountEntity.setBalance(new BigDecimal(0.00));
        accountEntity.setBalanceUnit(BaseContants.BALANCE_UNIT_RMB);
        accountEntity.setPayPassword(password);
        accountEntity.setAccountCode(BaseUtil.newAccountCode(userPhone));
        accountEntity.setCrtTime(new Date());
        int insertSelective = accountEntityMapper.insertSelective(accountEntity);
        if(insertSelective==1){
            outputObject.setRtnCode(OutputObject.SUCCESS_CODE);
            outputObject.setRtnMsg("账户创建成功");
            return;
        }else {
            outputObject.setRtnCode(OutputObject.SUCCESS_CODE);
            outputObject.setRtnMsg("未知的原因账户保存失败");
            return;
        }
    }
}
